package com.sh.data.engine.repository.dao.normalization.standard;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.sh.data.engine.repository.dao.normalization.standard.arg.DictionaryMappingReportArg;
import com.sh.data.engine.repository.dao.normalization.standard.entity.DictionaryMappingReportEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author jingchen.hr
 */
@Repository
public interface DictionaryMappingReportMapper extends BaseMapper<DictionaryMappingReportEntity> {

    /**
     * 按类型取映射报告列表
     *
     * @param projectId
     * @param datasourceBaseId
     * @return
     */
    default IPage<DictionaryMappingReportEntity> queryList(
        Long projectId, Long datasourceBaseId, Integer pageNum, Integer pageSize) {
        var objects = PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<DictionaryMappingReportEntity> query =
            new QueryWrapper<DictionaryMappingReportEntity>().lambda();
        query
            .eq(DictionaryMappingReportEntity::getRowState, 1)
            .eq(DictionaryMappingReportEntity::getDatasourceBaseId, datasourceBaseId)
            .eq(DictionaryMappingReportEntity::getProjectId, projectId)
            .orderByDesc(DictionaryMappingReportEntity::getUpdateTime);
        List<DictionaryMappingReportEntity> list = this.selectList(query);
//        PageInfo<DictionaryMappingReportEntity> pageInfo = new PageInfo<>();
//        pageInfo.setTotal(objects.getTotal());
//        pageInfo.setPageNum(pageNum);
//        pageInfo.setPageSize(pageSize);
//        pageInfo.setList(list);

        IPage<DictionaryMappingReportEntity> result = new Page<>();
        result.setTotal(objects.getTotal());
        result.setPages(objects.getPages());
        result.setCurrent(objects.getPageNum());
        result.setSize(objects.getPageSize());
        result.setRecords(list);
        return result;
    }

    default List<DictionaryMappingReportEntity> getAllReportList(
        Long projectId, Long datasourceBaseId) {
        LambdaQueryWrapper<DictionaryMappingReportEntity> query =
            new QueryWrapper<DictionaryMappingReportEntity>().lambda();
        query
            .eq(DictionaryMappingReportEntity::getRowState, 1)
            .eq(DictionaryMappingReportEntity::getDatasourceBaseId, datasourceBaseId)
            .eq(Objects.nonNull(projectId), DictionaryMappingReportEntity::getProjectId, projectId)
            .orderByDesc(DictionaryMappingReportEntity::getUpdateTime);

        return this.selectList(query);
    }

    /**
     * 批量删除
     *
     * @param tblIds
     */
    default int deleteByTblIdList(List<Long> tblIds) {
        if (CollectionUtils.isEmpty(tblIds)) {
            return 0;
        }

        LambdaQueryWrapper<DictionaryMappingReportEntity> query =
            new QueryWrapper<DictionaryMappingReportEntity>().lambda();
        query.in(DictionaryMappingReportEntity::getTblId, tblIds);
        return this.delete(query);
    }

    default int deleteByArgs(DictionaryMappingReportArg arg) {
        if (Objects.isNull(arg.getMapSource())) {
            return 0;
        }

        LambdaQueryWrapper<DictionaryMappingReportEntity> query =
            new QueryWrapper<DictionaryMappingReportEntity>().lambda();
        query.eq(DictionaryMappingReportEntity::getMapSource, arg.getMapSource());
        return this.delete(query);
    }

    default DictionaryMappingReportEntity getReportByProjectIdAndTblId(Long projectId, Long tblId) {

        LambdaQueryWrapper<DictionaryMappingReportEntity> query =
            new QueryWrapper<DictionaryMappingReportEntity>().lambda();
        query
            .eq(DictionaryMappingReportEntity::getProjectId, projectId)
            .eq(DictionaryMappingReportEntity::getTblId, tblId);
        return this.selectOne(query);
    }

    default DictionaryMappingReportEntity getReportByProjectIdAndTblInfo(
        Long projectId, String jdbcUrl, String dbName, String tblName) {

        LambdaQueryWrapper<DictionaryMappingReportEntity> query =
            new QueryWrapper<DictionaryMappingReportEntity>().lambda();
        query
            .eq(DictionaryMappingReportEntity::getProjectId, projectId)
            .eq(DictionaryMappingReportEntity::getJdbcUrl, jdbcUrl)
            .eq(DictionaryMappingReportEntity::getDbName, dbName)
            .eq(DictionaryMappingReportEntity::getTableName, tblName);
        List<DictionaryMappingReportEntity> multi = this.selectList(query);
        if (CollectionUtils.isNotEmpty(multi)) {
            return multi.get(0);
        }
        return null;
    }

    default DictionaryMappingReportEntity getReportByArgs(DictionaryMappingReportArg arg) {

        LambdaQueryWrapper<DictionaryMappingReportEntity> query =
            new QueryWrapper<DictionaryMappingReportEntity>().lambda();
        query
            .eq(
                Objects.nonNull(arg.getProjectId()),
                DictionaryMappingReportEntity::getProjectId,
                arg.getProjectId())
            .eq(
                Objects.nonNull(arg.getTblId()),
                DictionaryMappingReportEntity::getTblId,
                arg.getTblId())
            .eq(
                Objects.nonNull(arg.getRowState()),
                DictionaryMappingReportEntity::getRowState,
                arg.getRowState());
        List<DictionaryMappingReportEntity> list = this.selectList(query);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    default List<Long> getTblIdsByProjectId(Long projectId) {

        LambdaQueryWrapper<DictionaryMappingReportEntity> query =
            new QueryWrapper<DictionaryMappingReportEntity>().lambda();
        query
            .eq(DictionaryMappingReportEntity::getProjectId, projectId)
            .eq(DictionaryMappingReportEntity::getMapSource, 0);
        List<DictionaryMappingReportEntity> multi = this.selectList(query);
        List<Long> list = multi.stream().map(i -> i.getTblId()).collect(Collectors.toList());
        return list;
    }

    default Date getMaxUpdateTime(Long projectId) {

        LambdaQueryWrapper<DictionaryMappingReportEntity> query =
            new QueryWrapper<DictionaryMappingReportEntity>().lambda();
        query
            .eq(DictionaryMappingReportEntity::getRowState, 1)
            .eq(DictionaryMappingReportEntity::getProjectId, projectId)
            .orderByDesc(DictionaryMappingReportEntity::getUpdateTime);
        DictionaryMappingReportEntity one = this.selectOne(query);
        return Objects.isNull(one) ? null : one.getUpdateTime();
    }
}
